<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 子组件向父组件传递数据 -->
    <!-- 1. 通过this.$emit 自定义事件来传递 -->
    <!-- 2. 在子组件的标签上 通过@事件名来接收 -->
    
    <div id="app">
        <my-father></my-father>
    </div>

     <!-- 父组件 -->
     <template id="father">
        <div>
            <h2>
                我是父组件 --- {{ str }}
            </h2>
            <my-son @send="acceptStr"></my-son>
        </div>
    </template>

    <!-- 子组件 -->
    <template id="son">
        <div>
            <h4>
                我是子组件 --- {{ str }}
                <button @click="sendStr">点我传递数据</button>
            </h4>
        </div>
    </template>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
       
       let Son = {
            template: "#son",
            data() {
                return {
                    str: "我是子组件内部的数据"
                }
            },
            methods: {
                sendStr() {
                    // this.$emit(事件名称)  通过自定义事件的方式来传递数据
                    this.$emit("send", this.str)
                }
            }
        }



        let Father = {
            template: "#father",
            components: {
                "my-son": Son
            },
            data() {
                return {
                    str: ""
                }
            },
            methods: {
                acceptStr(data) {
                    this.str = data
                }
            }
        }


        new Vue({
            // 元素
            el: "#app",
            components: {
                "my-father": Father
            }
        })
    </script>
</body>
</html>